package com.hello915.googledirectionapi;

import java.util.List;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.hello915.googledirectionapi.route.Mode;
import com.hello915.googledirectionapi.route.OnRouteResultCallback;
import com.hello915.googledirectionapi.route.Parameter;
import com.hello915.googledirectionapi.route.Parameter.Builder;
import com.hello915.googledirectionapi.route.RoutePlanner;
import com.hello915.googledirectionapi.route.RouteResultStatus;
import com.hello915.googledirectionapi.route.result.ResponseBean;
import com.hello915.googledirectionapi.utils.MapUtil;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity implements OnMapReadyCallback {

	private MapFragment mapFragment;
	private GoogleMap googleMap; 
	private Polyline myRoute;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initMap();
	}
	
	private void initMap(){
		int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
		if(status == ConnectionResult.SUCCESS){ 
			mapFragment = (MapFragment) getFragmentManager()
					.findFragmentById(R.id.google_map);
			mapFragment.getMapAsync(this);
		}else{	
			Toast.makeText(this, "请开启Google Play Service", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onMapReady(GoogleMap map) {
		this.googleMap = map;
		map.getUiSettings().setAllGesturesEnabled(true);
		map.getUiSettings().setMapToolbarEnabled(true); // 路径规划，Place API
		map.getUiSettings().setCompassEnabled(true);
		map.getUiSettings().setIndoorLevelPickerEnabled(true);
		map.getUiSettings().setMyLocationButtonEnabled(true);
		map.getUiSettings().setZoomControlsEnabled(false);
		LatLng start = new LatLng(22.565224, 113.8677818);
		LatLng end = new LatLng(22.665224, 113.9677818);
		route(start, end);
	}

	private void route(final LatLng start, LatLng end){
		RoutePlanner pl = new RoutePlanner();
		if(start == null || end == null){
			Toast.makeText(this, "起点和终点不能为空", Toast.LENGTH_LONG).show();
			return;
		}
		Builder b = new Builder(start, end);
		b.mode(Mode.TRANSIT);
		Parameter parm = b.build();

		pl.route(parm, new OnRouteResultCallback() {

			@Override
			public void onStart(){
				
			}

			@Override
			public void onFailure(String msg) {
				Log.i("Route", msg);
				Toast.makeText(MainActivity.this, "Failure:" + msg, Toast.LENGTH_LONG).show();
			}

			@Override
			public void onCompleted(ResponseBean rb) {
				MapUtil mu = new MapUtil(googleMap);
				if(myRoute != null){
					myRoute.remove();
				}
				if(rb.status.equals(RouteResultStatus.OK.getVal())){
					List<LatLng> points = rb.routes.get(0).overview_polyline.getWayPoint();								
					myRoute = mu.addPolyline(points, Color.RED);
					mu.moveToCenter(start, null);
				}else{
					Toast.makeText(MainActivity.this, "找不到路径", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}
